package com.example.devsa_platform.pojo.doc;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.csv.CSVRecord;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;

import java.io.Serializable;
import java.util.Objects;

@Data
@AllArgsConstructor
@NoArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
@Document(indexName = "usage_community_info", createIndex = false)
public class UsageAndCommunityDoc implements Serializable {
    @Field(name = "stack_overflow_sites")
    private String stackOverflowSites;
    @Field(name = "so_visit_frequency")
    private String soVisitFrequency;
    @Field(name = "so_account")
    private String soAccount;
    @Field(name = "so_participation_frequency")
    private String soParticipationFrequency;
    @Field(name = "so_community_member")
    private String soCommunityMember;

    public UsageAndCommunityDoc(CSVRecord record) {
        this.stackOverflowSites = getValueOrDefault(record.get("NEWSOSites"));
        this.soVisitFrequency = getValueOrDefault(record.get("SOVisitFreq"));
        this.soAccount = getValueOrDefault(record.get("SOAccount"));
        this.soParticipationFrequency = getValueOrDefault(record.get("SOPartFreq"));
        this.soCommunityMember = getValueOrDefault(record.get("SOComm"));
    }

    private String getValueOrDefault(String value) {
        return Objects.equals(value, "NA") ? "No Answer" : value;
    }
}
